﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing;
using System.Windows.Forms;
using XCI.Config;
using XCI.Helper;
using XCI.Sys.Settings;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmStandardLogin : FrmBaseLogin
    {
        public FrmStandardLogin()
        {
            InitializeComponent();
        }

        protected override Control EditAccount => editAccount;

        protected override Control EditPassword => editPassword;

        protected override Control BtnLogin => btnLogin;

        protected override void LoadLoginSetting()
        {
            captionControl1.CaptionText = SysRuntime.Setting.Title;
            editAutoLogin.Checked = StandardLoginSetting.Instance.AutoLogin;
            editSavePwd.Checked = StandardLoginSetting.Instance.IsRememberPassword;
            btnRegUser.Visible = StandardLoginSetting.Instance.AllowUserRegister;
            editAccount.Text = StandardLoginSetting.Instance.RememberLoginName;
            if (StringHelper.IsBase64(StandardLoginSetting.Instance.RememberPassword))
            {
                editPassword.Text = EncryptHelper.SymmetricDecrypt(StandardLoginSetting.Instance.RememberPassword,
                    EncryptHelper.SecretKey);
            }

            if (StandardLoginSetting.Instance.AutoLogin)
            {
                ExecuteAutoLogin();
            }
        }

        protected override void SaveLoginSetting()
        {
            string loginName = editAccount.Text.Trim();
            string password = editPassword.Text;
            StandardLoginSetting.Instance.AutoLogin = editAutoLogin.Checked;
            StandardLoginSetting.Instance.IsRememberPassword = editSavePwd.Checked;

            if (editSavePwd.Checked)
            {
                StandardLoginSetting.Instance.RememberLoginName = loginName;
                StandardLoginSetting.Instance.RememberPassword =
                    EncryptHelper.SymmetricEncrypt(password, EncryptHelper.SecretKey);
            }
            else
            {
                StandardLoginSetting.Instance.RememberLoginName = null;
                StandardLoginSetting.Instance.RememberPassword = null;
            }

            ConfigFactory.Default.Save();
        }

        private void StandardLoginForm_Load(object sender, EventArgs e)
        {
            LoadLoginSetting();
            //NativeMethods.SetForegroundWindow(Handle);
        }

        private void btnRegUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBoxHelper.ShowMessage("正在开发");
        }

        private void btnFindPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBoxHelper.ShowMessage("正在开发");
        }

        private void btnRegUser_MouseEnter(object sender, EventArgs e)
        {
            ((LinkLabel)sender).LinkColor = Color.Red;
        }

        private void btnRegUser_MouseLeave(object sender, EventArgs e)
        {
            ((LinkLabel)sender).LinkColor = Color.Blue;
        }

        private void btnFindPwd_MouseEnter(object sender, EventArgs e)
        {
            ((LinkLabel)sender).LinkColor = Color.Red;
        }

        private void btnFindPwd_MouseLeave(object sender, EventArgs e)
        {
            ((LinkLabel)sender).LinkColor = Color.Blue;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            LoginCore();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void editSavePwd_CheckedChanged(object sender, EventArgs e)
        {
            if (!editSavePwd.Checked)
            {
                editAutoLogin.Checked = false;
            }
        }

        private void editAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (editAutoLogin.Checked)
            {
                editSavePwd.Checked = true;
            }
        }

        private void editLoginName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && editAccount.Text.Trim().Length > 0)
            {
                editPassword.Select();
                e.Handled = true;
            }
        }

        private void editPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnLogin.PerformClick();
                e.Handled = true;
            }
            else if (e.KeyChar == 27)
            {
                editAccount.Focus();
                editAccount.SelectionStart = editAccount.Text.Length;
                e.Handled = true;
            }
        }
    }
}